<template>
  <div class="wrap">
    <div class="top">
      <div class="toptitle">
        <!-- <div style="top: 8px; left: 10px">
          <img
            id="topimg"
            border="0"
            src="../assets/share.png"
            style="height: 36px; width: 36px; border-radius: 18px; display: "
          />
        </div> -->
        <p>PM-为您服务</p>
        <!-- <p>
          <router-link target="_blank" :to="{ path: '/message' }"
            >咨询</router-link
          >
        </p> -->
      </div>
    </div>
    <div class="center" ref="center">
      <!-- 自动回复 转人工-->
      <!-- 历史消息 -->
      <div class="content" v-for="(item, index) in historyList" :key="index">
        <div class="left" v-if="item.type === 1">
          <img :src="item.avatar" />
          <div class="talk">
            <div class="arrow"></div>
            <!-- <div class="msg">{{ item.msg }}</div> -->
            <pre v-html="item.msg" class="msg"></pre>
          </div>
        </div>
        <div class="right" v-if="item.type === 2">
          <div class="talk">
            <div class="arrow"></div>
            <div class="msg">{{ item.msg }}</div>
          </div>
          <img :src="item.avatar" />
        </div>
      </div>
    </div>
    <div class="bottom">
      <div class="toolBar">
        <div class="left">
          <el-popover
            placement="top-start"
            :width="480"
            trigger="click"
            ref="popoverEmoticon"
            v-model:visible="visible"
          >
            <div class="emjor">
              <MeEditorEmoticon
                ref="editorEmoticon"
                @selected="selecteEmoticon"
              />
            </div>
            <template #reference>
              <i class="iconfont icon-weixiao" @click="visible = !visible" />
            </template>
          </el-popover>
        </div>
        <div class="right">
          <p class="text-tips">
            <span>按Enter发送 / Shift+Enter 换行</span>
            <el-popover
              placement="top-end"
              title="说明"
              :width="300"
              trigger="hover"
            >
              <!-- <div>
                <p>
                  1.支持上传QQ及微信截图，在QQ或微信中截图后使用Ctrl+v上传图片。
                </p>
                <p>
                  2.支持浏览器及Word文档中的图片复制上传、复制后使用Ctrl+v上传图片。
                </p>
                <p>3.支持图片拖拽上传。</p>
                <p>4.支持文件上传 ( 文件小于100M ) 。</p>
                <p>5.按Enter发送 / Shift+Enter 换行。</p>
                <p>
                  6.注意：当文件正在上传时，请勿关闭网页或离开当前对话框，否则将导致文件停止上传或上传失败。
                </p>
              </div> -->
              <div>
                <p>按Enter发送 / Shift+Enter 换行。</p>
              </div>
              <template #reference>
                <i class="iconfont icon-i-info" />
              </template>
            </el-popover>
          </p>
        </div>
      </div>
      <textarea
        autofocus
        ref="textarea"
        rows="4"
        placeholder="你想要的聊点什么呢 ..."
        class="textarea"
        v-model.trim="editorText"
        @keydown="keydownEvent($event)"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { emojiList as emoji } from '../utils/emojis'
import MeEditorEmoticon from './editor/MeEditorEmoticon.vue'
import { getCurrentInstance, onUpdated, reactive, ref } from 'vue'
const { proxy } = getCurrentInstance() as any

const btnClick = () => {
  proxy.$message('发消息了')
}

const historyList = reactive([
  {
    type: 1,
    fromUserId: '1',
    userId: '2',
    msg: '你好，我想咨询一下项目',
    avatar:
      'https://im.gzydong.club/public/media/image/avatar/20220224/fa7cd682ba942a6d9f04218eb82b7e75_200x200.png',
  },
  {
    type: 2,
    fromUserId: '2',
    userId: '1',
    msg: '好的，你想咨询什么项目',
    avatar:
      'https://im.gzydong.club/public/media/image/avatar/20220102/e272a438c2ddb4933ed7b6e556a668a1_200x200.png',
  },
])

const visible = ref(false)
const editorText = ref('')
const selecteEmoticon = (data) => {
  // 回调把图片显示在输入框中
  let value = editorText.value
  let el = proxy.$refs.textarea
  let startPos = el.selectionStart
  let endPos = el.selectionEnd
  let newValue =
    value.substring(0, startPos) + data + value.substring(endPos, value.length)
  editorText.value = newValue
  if (el.setSelectionRange) {
    let index = startPos + data.length
    el.setSelectionRange(index, index)
    el.focus()
  }
  visible.value = false
}

// 键盘按下监听事件
const keydownEvent = (e) => {
  if (e.keyCode == 13 && editorText.value == '') {
    e.preventDefault()
  }
  const emoj = editorText.value
  // 判断是否发送的表情
  if (emoji.emojis[emoj]) {
    editorText.value = emoji.emojis[emoj]
  }
  // 回车发送消息
  if (e.keyCode == 13 && e.shiftKey == false && editorText.value != '') {
    // 此时应把数据发送到后台，发送成功后将数据显示
    historyList.push({
      type: 1,
      fromUserId: '1',
      userId: '1',
      msg: editorText.value,
      avatar:
        'https://im.gzydong.club/public/media/image/avatar/20220224/fa7cd682ba942a6d9f04218eb82b7e75_200x200.png',
    })
    editorText.value = ''
    setTimeout(() => {
      let container = proxy.$refs.center
      container.scrollTop = container.scrollHeight
    }, 100)
    e.preventDefault()
  }
}
</script>

<style lang="scss">
body {
  width: 100%;
  height: 100%;
  min-width: 800px;
  background: url(../assets/bg.png) no-repeat;
  background-size: 100% 100%;
}
.wrap {
  width: 66.8229vw;
  height: 85vh;
  min-height: 400px;
  min-width: 970px;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  background: #fff;
  border-radius: 4px;

  .top {
    padding: 10px;
    height: 40px;
    border: 1px solid #eee;
    .toptitle {
      width: 100%;
      display: flex;
      align-items: center;
      p {
        margin-left: 10px;
        font-size: 24px;
      }
    }
  }
  .center {
    padding: 20px 10px;
    height: 380px;
    max-height: 380px;
    overflow: auto;
    background: #ffffffcb;
    .content {
      .left {
        display: flex;
        align-items: center;
        margin-top: 10px;
        img {
          width: 30px;
          height: 30px;
          border-radius: 100%;
          margin-right: 10px;
        }
        .talk {
          display: flex;
          align-items: center;
          flex-direction: column;
          align-items: flex-start;
          box-sizing: border-box;
          width: 100%;
          position: relative;
          .arrow {
            position: absolute;
            width: 0;
            height: 0;
            font-size: 0;
            border: 5px solid;
            top: 6px;
            left: -10px;
            border-color: transparent #f5f5f5 transparent transparent;
          }
          .msg {
            display: flex;
            align-items: center;
            color: #3a3a3a;
            background: #f5f5f5;
            max-width: calc(100% - 50px);
            min-width: 30px;
            min-height: 30px;
            border-radius: 5px;
            padding: 5px;
          }
        }
      }
      .right {
        display: flex;
        align-items: center;
        margin-top: 10px;
        img {
          width: 30px;
          height: 30px;
          border-radius: 100%;
          margin-left: 10px;
        }
        .talk {
          display: flex;
          align-items: center;
          flex-direction: column;
          align-items: flex-end;
          box-sizing: border-box;
          width: 100%;
          position: relative;
          .arrow {
            position: absolute;
            width: 0;
            height: 0;
            font-size: 0;
            border: 5px solid;
            top: 6px;
            right: -10px;
            border-color: transparent transparent transparent #1ebafc;
          }
          .msg {
            display: flex;
            align-items: center;
            color: #fff;
            background: #1ebafc;
            max-width: calc(100% - 50px);
            min-width: 30px;
            min-height: 30px;
            border-radius: 5px;
            padding: 5px;
          }
        }
      }
    }
  }
  .bottom {
    position: relative;
    .toolBar {
      border: 1px solid #eee;
      height: 30px;
      line-height: 30px;
      padding: 0 10px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      .left {
        i {
          font-size: 16px;
          cursor: pointer;
        }
      }
      .right {
        .text-tips {
          float: right;
          margin-right: 15px;
          font-size: 12px;
          color: #ccc;
          i {
            font-size: 16px;
            cursor: pointer;
            margin-left: 5px;
            color: rgb(255, 181, 111);
          }
        }
      }
    }

    .textarea {
      width: calc(100% - 10px);
      width: -moz-calc(100% - 10px);
      width: -webkit-calc(100% - 10px);
      height: calc(100% - 100px);
      height: -moz-calc(100% - 100px);
      height: -webkit-calc(100% - 100px);
      border: 0 none;
      outline: none;
      resize: none;
      font-size: 15px;
      overflow-y: auto;
      color: #464545;
      padding: 5px;
      position: relative;
    }
  }
}
</style>
